 track1 = new Track();
 index = -1;
function test()
{
	var trackpoint1 = new Trackpoint();
		trackpoint1.setLatitude("51.9571996");
		trackpoint1.setLongitude("10.4342008");
		trackpoint1.setHeigth("227.0760000");
		trackpoint1.setTimestamp("2009-03-08T20:56:36Z");
		
	var trackpoint2 = new Trackpoint();
		trackpoint2.setLatitude("51.9572487");
		trackpoint2.setLongitude("10.4338646");
		trackpoint2.setHeigth("226.0039063");
		trackpoint2.setTimestamp("2009-03-08T20:57:36Z");
			
	var trackpoint3 = new Trackpoint();
		trackpoint3.setLatitude("51.9584052");
		trackpoint3.setLongitude("10.4339115");
		trackpoint3.setHeigth("226.1054688");
		trackpoint3.setTimestamp("2009-03-08T20:58:36Z");
		
	//var track1 = new Track();
		track1.addTrackpoint(trackpoint1);
		track1.addTrackpoint(trackpoint2);
		track1.addTrackpoint(trackpoint3);
		track1.setNumberOfPoints(3);
		//track1.initExport();
		window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
		
}

function gotFS(fileSystem) 
{
   fileSystem.root.getFile("track.gpx", {"create":true}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) 
{
   fileEntry.createWriter(gotFileWriter, fail);
}

function fail(error) 
{
  console.log(error.code);
}	

function gotFileWriter(writer)
{
	console.log("exportAsGPX-Function");
	
	var eof = false;
	writer.onwrite = function(evt){
		console.log("write Success");
	};
    var output = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"byHand\" version=\"1.1\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n<rte>\n";
	var trackpoints =track1.getTrackpoints();
    for(key in trackpoints)
    {
    	var tp = trackpoints[key];
    	alert(tp.getHeigth());
    	output += "<rtept lat=\""+tp.getLatitude()+"\" lon=\""+tp.getLongitude()+"\">\n<ele>"+tp.getHeigth()+"</ele>\n<time>"+tp.getTimestamp()+"</time>\n<name>Point "+key+"</name>\n</rtept>\n";
    }
    output+= "</rte>\n</gpx>\n";
	
    writer.write(output);
}

//function writeSuccess()
//{
//	index = index+1;
//    if(index == track1.getTrackpoints().length)
//    {
//    	console.log("writeEOF-Pfad, index= "+index);
//    	writeEOF();
//    }
//    else
//    {
//    	console.log("WritePoint-Pfad, index= "+index);
//    	writePoint();
//    }
//}
//
//function writePoint()
//{
//	console.log("writePoint-Function");
//	var trackpoints = track1.getTrackpoints();
//	//alert(typeof(track1));
//	//alert(typeof(trackpoints));
//	//alert(trackpoints.length);
//	//alert(typeof(trackpoints[index]));
//	//alert(typeof(trackpoints[index].getLatitude()));
//	//alert(trackpoints[index].getLatitude());
//	alert(typeof(writerObject));
//	//alert(writer.readyState);
//	writerObject.write("<rtept lat=\""+track1.getTrackpoints()[index].getLatitude()+"\" lon=\""+track1.getTrackpoints()[index].getLongitude()+"\">\n<ele>"+track1.getTrackpoints()[index].getHeigth()+"</ele>\n<time>"+track1.getTrackpoints()[index].getTimestamp()+"</time>\n<name>Point "+index+"</name>\n</rtept>/n")	;
//
//}
//
//function writeEOF()
//{
//	console.log("writeEOF-Function");
//	writerObject.write("</rte>/n</gpx>\n");
//}
